<template>
  <el-descriptions
      class="margin-top"
      title="公告"
      :column="4"
      border
  >
    <template #extra>
      <el-button @click="dialogFormVisible = true" type="primary" style="margin-right: 10px">新增</el-button>
      <el-dialog v-model="dialogFormVisible" title="新增公告" draggable>
        <el-form :model="form">
          <el-form-item label="公告内容" :label-width="formLabelWidth">
            <el-input v-model="form.content" autocomplete="off" type="textarea"/>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="announce">
          发布
        </el-button>
      </span>
        </template>
      </el-dialog>
    </template>
  </el-descriptions>


  <el-table :data="tableData" style="width: 100%">
    <el-table-column type="selection"/>
    <el-table-column label="公告id" prop="id"/>
    <el-table-column label="创建时间" prop="createTime"/>
    <el-table-column label="内容" prop="content"/>
    <el-table-column align="right">
      <template #header>
        <el-input v-model="keyword" placeholder="搜索公告"  @keyup.enter.native="search"/>

      </template>
      <template #default="scope">
        <el-button @click="handleEdit(scope.$index, scope.row)" type="primary" plain>
          编辑
        </el-button>
        <el-button
            type="danger"
            plain
            @click="handleDelete(scope.$index, scope.row)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination background layout="prev, pager, next" :total="total" @current-change="load"/>
  <el-dialog v-model="dialogVisible" title="修改公告" draggable>
    <el-form :model="form1">
      <el-form-item label="公告id" :label-width="formLabelWidth">
        <el-input v-model="form1.id" autocomplete="off" type="textarea"/>
      </el-form-item>
      <el-form-item label="创建时间" :label-width="formLabelWidth">
        <el-input v-model="form1.createTime" autocomplete="off" type="textarea"/>
      </el-form-item>
      <el-form-item label="内容" :label-width="formLabelWidth">
        <el-input v-model="form1.content" autocomplete="off" type="textarea"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="edit">
          修改公告
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {computed, Ref, ref, onMounted} from 'vue'
import NoticeAdmin from "@/class/NoticeAdmin";
import {findAllNoticeByAdmin} from "@/http";
import {userStore} from "@/store";
import axios from 'axios'


const total = ref(100)
const keyword = ref('')
const current = ref(1)
const userstore = userStore()
const form: Ref<NoticeAdmin> = ref<NoticeAdmin>({
  id: "",
  createTime: "",
  content: "",
  creatorId: userstore.admin_id,
});
const form1: Ref<NoticeAdmin> = ref<NoticeAdmin>({
  id: "",
  createTime: "",
  content: "",
  creatorId: userstore.admin_id,
});

const currentChange = (val: number) => {
  console.log(val)
}

const pre_form = ref({
  id: "",
  createTime: "",
  content: "",
  creatorId: userstore.admin_id,
})
const now_form = ref({
  id: "",
  createTime: "",
  content: "",
  creatorId: userstore.admin_id,
})

const dialogFormVisible = ref(false)
const dialogVisible = ref(false)
const formLabelWidth = '140px'

const handleEdit = (index: number, row: NoticeAdmin) => {
  dialogVisible.value = true
  console.log("当前id:" + row.id)
  form1.value = row
}
const edit = () => {
  dialogVisible.value = false
  pre_form.value = form1.value
  now_form.value = form1.value
  axios.put('/NoticeAdmin', form1.value).then(function (res) {
    console.log("修改的结果" + res)
    load(current.value)
    now_form.value = form1.value
  })
  console.log(form.value.id)
}
const cancel = () => {
  now_form.value = pre_form.value
  dialogVisible.value = false
  load(current.value)
}

const handleDelete = (index: number, row: NoticeAdmin) => {
  axios.delete('/NoticeAdmin/' + row.id).then(function (res) {
    console.log(res)
    load(current.value)
  })

  console.log(index, row.id)
}

const tableData = ref<any[]>([])

const load = async (val: number) => {
  let data = await findAllNoticeByAdmin(10, val - 1)
  tableData.value = data.data.content
  console.log(tableData.value)
  current.value = val
  total.value = data.data.totalElements
  console.log(total.value)
}


onMounted(async () => {
  await load(current.value)
})
const announce = async () => {
  dialogFormVisible.value = false
  await
      axios.post('/NoticeAdmin/addNotice', form.value)
          .then(function (res) {
            console.log(res.data)
            load(current.value)
            form1.value = {
              id: "",
              createTime: "",
              content: "",
              creatorId: '',
            }
          })
}
const search = () =>{
  axios.post('/NoticeAdmin/searchNotice?content='+keyword.value)
      .then(function(res){
        tableData.value = res.data.data
      })
}
</script>
